<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<div style="text-align: center;font-size: 18px;font-weight: 600;margin-bottom: 15px;">
				{{title}}
			</div>
			<el-form-item label="任务标题" prop="title">
				<el-input v-model="form.title" placeholder="事件标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="主办人" prop="holder">
						<el-select v-model="form.holder" style="width: 100%">
							<el-option v-for="item in userList" :key="item.accountId" :value="item.accountId" :label="item.userName"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="经办人" prop="operatorArr">
						<el-select v-model="form.operatorArr" style="width: 100%" multiple>
							<el-option v-for="item in userList" :key="item.accountId" :value="item.accountId" :label="item.userName"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.beginTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="任务要求" >
				<sc-editor v-model="form.remark" placeholder="请输入" :templates="templates" :height="400" clearable  style="width: 100%;"></sc-editor>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'supervision'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import {defineAsyncComponent} from "vue";
import AttachMenu from "@/views/module/attachMenu";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			mode: "add",
			templates: [],
			fileList:[],
			userList:[],
			title:"",
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '任务分解对话框',
				edit: '任务分解对话框',
			},
			form: {
				supervisionId:'',
				title: '',
				holder:"",
				operator:'',
				operatorArr:[],
				beginTime:'',
				endTime:'',
				remark: '',
				attachId:'',
			},
			rules: {
				title: [
					{required: true, message: '任务标题不能为空', trigger: 'blur'}
				],
				holder: [
					{required: true, message: '主办人不能为空', trigger: 'blur'}
				],
				operatorArr: [
					{required: true, message: '经办人不能为空', trigger: 'blur'}
				],
				beginTime: [
					{required: true, message: '开始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '结束时间不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		async getSupervisionHandedUserForSelect(){
			let res = await this.$API.supervision.supervisionConfig.getSupervisionHandedUserForSelect.get({supervisionId:this.form.supervisionId});
			if (res.code == 200) {
				this.userList = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					if(this.form.operatorArr!=null)
					{
						this.form.operator = this.form.operatorArr.join(",");
					}
					var res = await this.$API.supervision.supervisionProcess.insertSupervisionProcess.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		setData(data) {
			this.form.supervisionId = data.supervisionId
			this.title = data.title
			this.getSupervisionHandedUserForSelect();
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
